/*
 * Copyright 2025-2026 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package hawk.example.bookstore.repository.data;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.ThreadLocalRandom;

/**
 * This class is responsible for
 *
 * @author Hawk
 */
public class LocalDateTimeRangeGenerator {
    private final LocalDateTime minDateTime;
    private final LocalDateTime maxDateTime;
    private final ThreadLocalRandom random = ThreadLocalRandom.current();

    /**
     * @param minDateTime 最小时间（包含）
     * @param maxDateTime 最大时间（包含）
     */
    public LocalDateTimeRangeGenerator(LocalDateTime minDateTime, LocalDateTime maxDateTime) {
        this.minDateTime = minDateTime;
        this.maxDateTime = maxDateTime;
    }

    /**
     * 生成指定范围内的随机LocalDateTime
     *
     * @return 范围内的随机时间
     */
    public LocalDateTime randomLocalDateTime() {
        long secondsBetween = ChronoUnit.SECONDS.between(minDateTime, maxDateTime);
        long randomSeconds = ThreadLocalRandom.current().nextLong(secondsBetween + 1);
        return minDateTime.plusSeconds(randomSeconds).truncatedTo(ChronoUnit.MILLIS);
    }
}
